<?php

namespace app\merchant\controller;

use app\common\controller\AdminBase;
use app\common\controller\TestBase;
use app\common\model\CardConsume;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardStatis;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserGet;
use app\common\model\CardUserGetAli;
use app\common\model\CardUserUpdate;

use app\common\model\CardUserUpdateAli;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Reporttest extends TestBase
{
    public function index()
    {
        return $this->fetch();
    }

    //获取本会员余额变动详情
    public function cardusbalance()
    {
        $get_id = request()->param('get_id');
        $CardUserGet = new CardUserGet();
        $CardUserUpdate = new CardUserUpdate();
        $p_data = request()->get();
        unset($p_data['page']);
        unset($p_data['card_id']);
        unset($p_data['get_id']);
        if ($p_data) {
            if ($p_data['type'] != '') {
                $map['type'] = $p_data['type'];
            }
            if ($p_data['hx_fs'] != '') {
                $map['hx_fs'] = $p_data['hx_fs'];
            }
            if ($p_data['starttime'] !== '') {
                $starttime = strtotime($p_data['starttime']);
            } else {
                $starttime = 0;
            }
            if ($p_data['endtime'] !== '') {
                $endtime = strtotime($p_data['endtime']) + 86399;
            } else {
                $endtime = strtotime(date('Y-m-d H:i:s'));
            }
            $map['create_time'] = array(array('egt', $starttime), array('elt', $endtime), 'and');
        } else {
            $p_data['type'] = ['in', ['1,2']];
            $p_data['starttime'] = '';
            $p_data['endtime'] = '';
            $p_data['hx_fs'] = '请选择';
        }
        $arr = $CardUserGet->where('id', $get_id)->field('openid,card_id,code')->find();
        $map['openid'] = $arr['openid'];
        $map['card_id'] = $arr['card_id'];
        $map['code'] = $arr['code'];
        $map['add_balance|del_balance'] = ['neq', 0];

        $list = $CardUserUpdate->where($map)->order('id desc')->paginate(15, false, ['query' => request()->param()]);
        $this->assign('list', $list);
        $this->assign('p_data', $p_data);
        return $this->fetch();
    }

    //获取本会员余额变动详情
    public function alicardsbalance()
    {
        $get_id = request()->param('get_id');
        $CardUserGet = new CardUserGetAli();
        $CardUserUpdate = new CardUserUpdateAli();
        $p_data = request()->get();
        unset($p_data['page']);
        unset($p_data['user_id']);
        unset($p_data['get_id']);
        if ($p_data) {
            if ($p_data['type'] != '') {
                $map['type'] = $p_data['type'];
            }
            if ($p_data['hx_fs'] != '') {
                $map['hx_fs'] = $p_data['hx_fs'];
            }
            if ($p_data['starttime'] !== '') {
                $starttime = strtotime($p_data['starttime']);
            } else {
                $starttime = 0;
            }
            if ($p_data['endtime'] !== '') {
                $endtime = strtotime($p_data['endtime']) + 86399;
            } else {
                $endtime = strtotime(date('Y-m-d H:i:s'));
            }
            $map['create_time'] = array('between', '' . $starttime . ',' . $endtime . '');
        } else {
            $p_data['type'] = ['in', ['1,2']];
            $p_data['starttime'] = '';
            $p_data['endtime'] = '';
            $p_data['hx_fs'] = '请选择';
        }
        $arr = $CardUserGet->where('id', $get_id)->field('user_id,template_id,code')->find();
        $map['user_id'] = $arr['user_id'];
        $map['template_id'] = $arr['template_id'];
//        $map['code'] = $arr['code'];
        $map['add_balance|del_balance'] = ['neq', 0];
        var_dump(json_encode($map));
        $list = $CardUserUpdate->where($map)->order('id desc')->paginate(15, false, ['query' => request()->param()]);
//        dump($CardUserUpdate->getLastSql());
        $this->assign('list', $list);
        $this->assign('p_data', $p_data);
        return $this->fetch();
    }

    //获取本会员卡券核销详情
    public function cardsconsume()
    {
        $arr = request()->param();
        $CardConsume = new CardConsume();
        $map['openid'] = $arr['openid'];
        $map['card_id'] = $arr['card_id'];
        $map['code'] = $arr['code'];
        $list = $CardConsume->where($map)->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //会员卡核销详情
    public function reportcardmember()
    {
        $CardUserUpdate = new CardUserUpdate();
        $CardMember = new CardMember();
        $request = request();
        //未创建门店
        if ($this->get_mer_mid() !== '') {
            $mid = $this->get_mer_mid();
        } else {
            $mid = null;
        }
        $p_data = $request->get();
        unset($p_data['page']);
        unset($p_data['card_id']);
        if ($p_data) {
            if ($p_data['mid'] != '') {
                $mid = $p_data['mid'];
            }
        } else {
            $p_data['mid'] = '';
        }
        //会员卡列表
        if ($this->get_mer_mid() !== '') {
            $map['mid'] = ['in', $this->get_mer_mid()];
        } else {
            $map['mid'] = null;
        }
        $list = $CardMember->where($map)->paginate(10);
        //今日统计
        $today = strtotime(date("Y-m-d"), time());
        $enday = $today + 86399;
        $day['create_time'] = ['between', [$today, $enday]];
        $day['mid'] = ['in', $mid];
        $day['type'] = 2;
        //本周统计
        $toweek = strtotime(date('Y-m-d', strtotime("this week Monday", time())));
        $enweek = strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
        $week['create_time'] = ['between', [$toweek, $enweek]];
        $week['mid'] = ['in', $mid];
        $week['type'] = 2;
        //本月统计
        $tomonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $enmonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $month['create_time'] = ['between', [$tomonth, $enmonth]];
        $month['mid'] = ['in', $mid];
        $month['type'] = 2;
        //数据输出
        //日
        $daysum['numsum'] = $CardUserUpdate->where($day)->count('id');
        $daysum['bonsum'] = $CardUserUpdate->where($day)->sum('del_bonus');
        $daysum['balsum'] = $CardUserUpdate->where($day)->sum('del_balance');
        //周
        $weeksum['numsum'] = $CardUserUpdate->where($week)->count('id');
        $weeksum['bonsum'] = $CardUserUpdate->where($week)->sum('del_bonus');
        $weeksum['balsum'] = $CardUserUpdate->where($week)->sum('del_balance');
//        dump($week);
        //月
        $monthsum['numsum'] = $CardUserUpdate->where($month)->count('id');
        $monthsum['bonsum'] = $CardUserUpdate->where($month)->sum('del_bonus');
        $monthsum['balsum'] = $CardUserUpdate->where($month)->sum('del_balance');
        if ($mid == '') {
            //日
            $daysum['numsum'] = '';
            $daysum['bonsum'] = '';
            $daysum['balsum'] = '';
            //周
            $weeksum['numsum'] = '';
            $weeksum['bonsum'] = '';
            $weeksum['balsum'] = '';
            //月
            $monthsum['numsum'] = '';
            $monthsum['bonsum'] = '';
            $monthsum['balsum'] = '';
        }
        $Merchant = new Merchant();
        $mab['id'] = ['in', $this->get_mer_mid()];
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach', $mach);
        $this->assign('daysum', $daysum);
        $this->assign('weeksum', $weeksum);
        $this->assign('monthsum', $monthsum);
        $this->assign('p_data', $p_data);
        $this->assign('list', $list);
        return $this->fetch();
    }


    //卡券核销记录
    public function reportcardgenerallist($card_id = '')
    {

        $request = request();
        $CardConsume = new CardConsume();
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['mchid'])) {
                $mchid = $param['mchid'];
            } else {
                $mchid = $this->get_mer_mid();
            }
            if (!empty($param['con_username'])) {
                $map['a.con_username'] = array('like', '%' . trim($param['con_username']) . '%');
            }
            if (!empty($param['card_id'])) {
                $map['a.card_id'] = $param['card_id'];
            }
            if (!empty($param['code'])) {
                $map['a.code'] = $param['code'];
            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month");
                $end_time = time();
            }
            $map['a.mid'] = ['in', $mchid];
            $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time), 'and');
            if ($param['type'] == "search") {
                $list = $CardConsume->alias('a')
                    ->join('wn_card_general b', 'a.card_id=b.card_id', 'left')
                    ->join('wn_merchant c', 'a.mid=c.id', 'left')
                    ->where($map)
                    ->order('create_time desc')
                    ->field('a.id,a.mid,a.card_id,a.source,a.code,a.openid,a.con_username,a.create_time,b.title as card_title,c.merchant')
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->select();
                $re_param["total"] = $CardConsume->alias('a')->where($map)->count();
                //获取的记录
                $re_param["rows"] = collection($list)->toArray();
                return json($re_param);

            }elseif ($param['type'] == "out_excel") {
                $pub = new \app\common\controller\Pub();
                $header = ['门店', '订单号', '姓名', '卡号', '消费金额', '消费渠道', '消费时间', '操作人']; //表头,名称可自定义

                $list = $CardConsume->where($map)->order('')->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize']);
                $excel = $pub->exportExcel( $header, $list, 'reportcardgenerallist' . time() . rand(1000, 9999), "卡券核销记录", "reportcardgenerallist", false);
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }
        }else{
            $param = $request->param();
            if (isset($param['card_id'])){
                $card['card_id'] = $param['card_id'];
            }else{
                $card['card_id'] = '';
            }
            //门店
            $Merchant = new Merchant();
            //未创建门店
            if ($this->get_mer_mid() !== '') {
                $mab['id'] = ['in', $this->get_mer_mid()];
            } else {
                $mab['id'] = null;
            }
            $mach = $Merchant->where($mab)->select();
            $this->assign('card',$card);
            $this->assign('mach', $mach);
            return $this->fetch();
        }

    }

    //卡券核销详情
    public function reportcardgeneral(Request $request)
    {
        $CardGeneral = new CardGeneral();
        $CardConsume = new CardConsume();
        $CardUserGet = new CardUserGet();
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['card_name'])) {
                $mapcardname['title'] = trim($param['card_name']);
                $map['a.card_id'] = $CardGeneral->where($mapcardname)->value('card_id');
            }
            if (!empty($param['machname'])) {
                $map['a.mid'] = getmid(trim(request()->post('machname')));
            }else{
                $map['a.mid'] = array('in', $this->get_mer_mid());
            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month");
                $end_time = time();
            }
            $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time));
            if ($param['type'] == "search") {
                //卡券列表
                $cardslist = $CardGeneral->alias('a')
//                    ->join('wn_card_consume b', 'a.card_id=b.card_id', 'left')
//                    ->join('wn_card_user_get c', 'a.card_id=c.card_id', 'left')
                    ->where($map)
                    ->order('a.create_time desc')
                    ->field('a.id,a.mid,a.card_type,a.card_id,a.create_time,a.title,a.stocks,a.getsume as get_sume,a.consume as con_sume,a.least_cost,a.reduce_cost')
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->select();
//                return json($cardslist);
                $list = [];
                foreach ($cardslist as $k => $v) {
                    $maplist['card_id'] = $v['card_id'];
                    $list[$k] = $v;
                    $list[$k]['sub_title'] = $v['card_type'] == 'CASH' ? '满' . $v['least_cost'] . '减' . $v['reduce_cost'] . '代金券' : $v['title'];;
                }
                //统计
                $re_param["total"] = $CardGeneral->alias('a')->where($map)->count();
                //获取的记录
                $re_param["rows"] = $list;
                return json($re_param);
            }elseif ($param['type'] == "out_excel") {

            }
        }else {
            $map['mid'] = array('in', $this->get_mer_mid());
            $count['card_sume'] = $CardGeneral->where($map)->count();
            $count['get_sume'] = $CardUserGet->where($map)->count();
            $count['con_sume'] = $CardConsume->where($map)->count();
            //门店
            $Merchant = new Merchant();
            //未创建门店
            if ($this->get_mer_mid() !== '') {
                $mab['id'] = ['in', $this->get_mer_mid()];
            } else {
                $mab['id'] = null;
            }
            //时间
            $today = strtotime(date('Y-m-d'));
            $p_data['search_date'] = date('Y-m-d H:i:s',$today)." - ".date('Y-m-d H:i:s');
            $mach = $Merchant->where($mab)->select();
            $this->assign('p_data',$p_data);
            $this->assign('mach', $mach);
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    //核销记录
    public function getcardconlist()
    {
        if (Request()->isAjax()) {
            $p_data = request()->post();

            $CardConsume = new CardConsume();
//            $p_data['pageNum'] = 1;
//            $p_data['pageSize'] = 20;
            $page = ($p_data['pageNum'] - 1) * 20;
            $map = [];
            if ($p_data) {
                if ($p_data['search_date'] !== '') {
                    $searchdate = explode(" - ", request()->param('search_date'));
                    $starttime = strtotime($searchdate[0]);
                    $endtime = strtotime($searchdate[1]);
                } else {
                    $starttime = strtotime(date('Y-m-d'));
                    $endtime = time();
                }
                //门店
                if ($p_data['machname'] !== '') {
                    $map['mid'] = getmid(trim(request()->post('machname')));
                } else {
                    $map['mid'] = array('in', $this->get_mer_mid());
                }
                //时间
                $map['create_time'] = array('between', '' . $starttime . ',' . $endtime . '');
                if ($p_data['card_id'] !== '') {
                    $map['card_id'] = $p_data['card_id'];
                }
            }
            $list = $CardConsume->where($map)->limit($page, 20)->select();
            $count = $CardConsume->where($map)->count();
            $sql = $CardConsume->getLastSql();
            if ($list) {
                $data['code'] = 0;
                $data['message'] = 'success';
                $data['data']['list'] = $list;
                $data['data']['count'] = $count;
                $data['pageCount'] = ceil($count / 20);
                $data['pageNum'] = $p_data['pageNum'];
                $data['sql'] = $sql;
                $data['p_data'] = $p_data;
            } else {
                $data['code'] = 0;
                $data['message'] = 'error';
                $data['data']['list'] = '';
                $data['data']['count'] = $count;
                $data['pageCount'] = ceil($count / 20);
                $data['pageNum'] = 0;
                $data['sql'] = $sql;
                $data['p_data'] = $p_data;
            }
            return json($data);
        }
    }

    //领取记录
    public function getcardgetlist()
    {
        if (Request()->isAjax()) {
            $p_data = request()->post();
//            $p_data['pageNum'] = 1;
//            $p_data['pageSize'] = 20;
//            $p_data['statu'] = '';
            $CardUserGet = new CardUserGet();
            $page = ($p_data['pageNum'] - 1) * 20;
            $map = [];
            if ($p_data) {
                if ($p_data['search_date'] !== '') {
                    $searchdate = explode(" - ", request()->param('search_date'));
                    $starttime = strtotime($searchdate[0]);
                    $endtime = strtotime($searchdate[1]);
                } else {
                    $starttime = strtotime(date('Y-m-d'));
                    $endtime = time();
                }
                $map['create_time'] = array('between', '' . $starttime . ',' . $endtime . '');      //时间
                $map['card_id'] = $p_data['card_id'];               //卡券
                //门店
                if ($p_data['machname'] !== '') {
                    $map['mid'] = getmid(trim(request()->post('machname')));
                } else {
                    $map['mid'] = array('in', $this->get_mer_mid());
                }
                //领取状态
                if ($p_data['have'] !== '') {
                    if ($p_data['have'] == 1) {
                        $map['have'] = 1;
                    } else {
                        $map['have'] = 0;
                    }
                }
                //核销状态
                if ($p_data['statu'] !== '') {
                    if ($p_data['statu'] == 1) {
                        $map['statu'] = 2;
                    } else {
                        $map['statu'] = ['neq', 2];
                    }
                }
            }
            $list = [];
            $userlist = $CardUserGet->where($map)->limit($page, 20)->field('openid,nickname,statu,code,create_time,have,card_id')->select();
            foreach ($userlist as $k => $v) {
                $list[$k]['openid'] = $v['openid'];
                $list[$k]['card_id'] = $v['card_id'];
                $list[$k]['nickname'] = get_user_nickname($v['openid']);
                $list[$k]['statu'] = $v['statu'] == 0 ? '未核销' : '已核销';
                $list[$k]['code'] = $v['code'];
                $list[$k]['have'] = $v['have'] == 1 ? '已领取' : '已删除';
                $list[$k]['create_time'] = $v['create_time'];
            }
            $sql = $CardUserGet->getLastSql();
            $count = $CardUserGet->where($map)->count();
            if ($list) {
                $data['code'] = 0;
                $data['message'] = 'success';
                $data['data']['list'] = $list;
                $data['data']['count'] = $count;
                $data['pageCount'] = ceil($count / 20);;
                $data['pageNum'] = $p_data['pageNum'];
                $data['sql'] = $sql;
                $data['map'] = $map;
            } else {
                $data['code'] = 0;
                $data['message'] = 'error';
                $data['data']['list'] = '';
                $data['data']['count'] = 0;
                $data['pageCount'] = 0;
                $data['pageNum'] = 0;
            }
            return json($data);
        }
    }


    //会员卡统计概览
    public function cardsurvey(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['mchid'])) {
                $mchid = $param['mchid'];
            } else {
                $mchid = $this->get_mer_mid();
            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month");
                $end_time = time();
            }
            if ($param['type'] == "search") {

                $map['statis_times'] = array(array('egt', $start_time), array('elt', $end_time), 'and');
                $map['mid'] = ['in', $mchid];
                $field = "sum(rec_card_num) as rec_card_num,sum(ret_card_num) as ret_card_num,sum(rec_fee) as rec_fee,sum(give_fee) as give_fee,sum(wx_rec_fee) as wx_rec_fee,sum(wx_give_fee) as wx_give_fee,sum(wxonline_rec_fee) as wxonline_rec_fee,sum(wxonline_give_fee) as wxonline_give_fee, sum(ali_rec_fee) as ali_rec_fee,sum(ali_give_fee) as ali_give_fee,sum(alionline_rec_fee) as alionline_rec_fee,sum(alionline_give_fee) as alionline_give_fee,sum(cash_rec_fee) as cash_rec_fee,sum(cash_give_fee) as cash_give_fee,sum(con_fee) as con_fee,sum(con_order) as con_order,sum(give_bonus) as give_bonus, sum(con_bonus) as con_bonus,statis_time,month,mid";
                $CardStatis = new CardStatis();
                if ($param['sch_type'] == "day") {
                    $list = $CardStatis->where($map)->field($field)->group('statis_time')->select();
                } elseif ($param['sch_type'] == "mon") {
                    $list = $CardStatis->where($map)->field($field)->group('month')->select();

                } elseif ($param['sch_type'] == "mach") {
                    $Merchant = new Merchant();
                    $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                    $list = $CardStatis->where($map)->field($field)->group('mid')->select();
                    foreach ($list as $k => $v) {
                        $list[$k]['mchname'] = $mchinfo[$v['mid']];

                    }
                }
                //获取的记录
                $re_param = $list;
                return json($re_param);
            } elseif ($param['type'] == "out_excel") {

                $map['statis_times'] = array(array('egt', $start_time), array('elt', $end_time), 'and');
                $map['mid'] = ['in', $mchid];
                $CardStatis = new CardStatis();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                $pub = new \app\common\controller\Pub();

                if ($param['sch_type'] == "day") {
                    $field = "sum(rec_card_num) as rec_card_num,sum(ret_card_num) as ret_card_num,sum(rec_fee) as rec_fee,sum(give_fee) as give_fee,sum(wx_rec_fee) as wx_rec_fee,sum(wx_give_fee) as wx_give_fee,sum(wxonline_rec_fee) as wxonline_rec_fee,sum(wxonline_give_fee) as wxonline_give_fee, sum(ali_rec_fee) as ali_rec_fee,sum(ali_give_fee) as ali_give_fee,sum(alionline_rec_fee) as alionline_rec_fee,sum(alionline_give_fee) as alionline_give_fee,sum(cash_rec_fee) as cash_rec_fee,sum(cash_give_fee) as cash_give_fee,sum(con_fee) as con_fee,sum(con_order) as con_order,sum(give_bonus) as give_bonus, sum(con_bonus) as con_bonus,statis_time";
                    $list = $CardStatis->where($map)->field($field)->group('statis_time')->select();
                    foreach ($list as $k => $v) {
                        $list2[$k]['statis_time'] = $v['statis_time'];
                        $list2[$k]['rec_card_num'] = $v['rec_card_num'];
                        $list2[$k]['ret_card_num'] = $v['ret_card_num'];
                        $list2[$k]['rec_fee'] = $v['rec_fee'];
                        $list2[$k]['give_fee'] = $v['give_fee'];
                        $list2[$k]['wx_rec_fee'] = $v['wx_rec_fee'];
                        $list2[$k]['wx_give_fee'] = $v['wx_give_fee'];
                        $list2[$k]['wxonline_rec_fee'] = $v['wxonline_rec_fee'];
                        $list2[$k]['wxonline_give_fee'] = $v['wxonline_give_fee'];
                        $list2[$k]['ali_rec_fee'] = $v['ali_rec_fee'];
                        $list2[$k]['ali_give_fee'] = $v['ali_give_fee'];
                        $list2[$k]['alionline_rec_fee'] = $v['alionline_rec_fee'];
                        $list2[$k]['alionline_give_fee'] = $v['alionline_give_fee'];
                        $list2[$k]['cash_rec_fee'] = $v['cash_rec_fee'];
                        $list2[$k]['cash_give_fee'] = $v['cash_give_fee'];
                        $list2[$k]['con_fee'] = $v['con_fee'];
                        $list2[$k]['con_order'] = $v['con_order'];
                        $list2[$k]['give_bonus'] = $v['give_bonus'];
                        $list2[$k]['con_bonus'] = $v['con_bonus'];
                    }
                    $header = ['统计时间', '领卡数量', '退卡数量', '实充金额', '赠送金额', '微信实充金额', '微信充值赠送金额', '微信自助实充金额', '微信自助充值赠送金额', '支付宝实充金额', '支付宝赠送金额', '支付宝自助实充金额', '支付宝自助充值赠送金额', '现金实充金额', '现金充值赠送金额', '消费金额', '消费次数', '赠送积分', '使用积分']; //表头,名称可自定义
                    $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2, 'cardsurvey_day' . time() . rand(1000, 9999), "会员概览数据-日统计", "cardsurvey_month", false);;
                } elseif ($param['sch_type'] == "mon") {

                    $field = "sum(rec_card_num) as rec_card_num,sum(ret_card_num) as ret_card_num,sum(rec_fee) as rec_fee,sum(give_fee) as give_fee,sum(wx_rec_fee) as wx_rec_fee,sum(wx_give_fee) as wx_give_fee,sum(wxonline_rec_fee) as wxonline_rec_fee,sum(wxonline_give_fee) as wxonline_give_fee, sum(ali_rec_fee) as ali_rec_fee,sum(ali_give_fee) as ali_give_fee,sum(alionline_rec_fee) as alionline_rec_fee,sum(alionline_give_fee) as alionline_give_fee,sum(cash_rec_fee) as cash_rec_fee,sum(cash_give_fee) as cash_give_fee,sum(con_fee) as con_fee,sum(con_order) as con_order,sum(give_bonus) as give_bonus, sum(con_bonus) as con_bonus,month";
                    $list = $CardStatis->where($map)->field($field)->group('month')->select();
                    foreach ($list as $k => $v) {
                        $list2[$k]['month'] = $v['month'];
                        $list2[$k]['rec_card_num'] = $v['rec_card_num'];
                        $list2[$k]['ret_card_num'] = $v['ret_card_num'];
                        $list2[$k]['rec_fee'] = $v['rec_fee'];
                        $list2[$k]['give_fee'] = $v['give_fee'];
                        $list2[$k]['wx_rec_fee'] = $v['wx_rec_fee'];
                        $list2[$k]['wx_give_fee'] = $v['wx_give_fee'];
                        $list2[$k]['wxonline_rec_fee'] = $v['wxonline_rec_fee'];
                        $list2[$k]['wxonline_give_fee'] = $v['wxonline_give_fee'];
                        $list2[$k]['ali_rec_fee'] = $v['ali_rec_fee'];
                        $list2[$k]['ali_give_fee'] = $v['ali_give_fee'];
                        $list2[$k]['alionline_rec_fee'] = $v['alionline_rec_fee'];
                        $list2[$k]['alionline_give_fee'] = $v['alionline_give_fee'];
                        $list2[$k]['cash_rec_fee'] = $v['cash_rec_fee'];
                        $list2[$k]['cash_give_fee'] = $v['cash_give_fee'];
                        $list2[$k]['con_fee'] = $v['con_fee'];
                        $list2[$k]['con_order'] = $v['con_order'];
                        $list2[$k]['give_bonus'] = $v['give_bonus'];
                        $list2[$k]['con_bonus'] = $v['con_bonus'];
                    }
                    $header = ['统计时间', '领卡数量', '退卡数量', '实充金额', '赠送金额', '微信实充金额', '微信充值赠送金额', '微信自助实充金额', '微信自助充值赠送金额', '支付宝实充金额', '支付宝赠送金额', '支付宝自助实充金额', '支付宝自助充值赠送金额', '现金实充金额', '现金充值赠送金额', '消费金额', '消费次数', '赠送积分', '使用积分']; //表头,名称可自定义
                    $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2, 'cardsurvey_month' . time() . rand(1000, 9999), "会员概览数据-月度统计", "cardsurvey_month", false);;

                } elseif ($param['sch_type'] == "mach") {

                    $field = "sum(rec_card_num) as rec_card_num,sum(ret_card_num) as ret_card_num,sum(rec_fee) as rec_fee,sum(give_fee) as give_fee,sum(wx_rec_fee) as wx_rec_fee,sum(wx_give_fee) as wx_give_fee,sum(wxonline_rec_fee) as wxonline_rec_fee,sum(wxonline_give_fee) as wxonline_give_fee, sum(ali_rec_fee) as ali_rec_fee,sum(ali_give_fee) as ali_give_fee,sum(alionline_rec_fee) as alionline_rec_fee,sum(alionline_give_fee) as alionline_give_fee,sum(cash_rec_fee) as cash_rec_fee,sum(cash_give_fee) as cash_give_fee,sum(con_fee) as con_fee,sum(con_order) as con_order,sum(give_bonus) as give_bonus, sum(con_bonus) as con_bonus,mid";
                    $list = $CardStatis->where($map)->field($field)->group('mid')->select();
                    foreach ($list as $k => $v) {
                        $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                        $list2[$k]['rec_card_num'] = $v['rec_card_num'];
                        $list2[$k]['ret_card_num'] = $v['ret_card_num'];
                        $list2[$k]['rec_fee'] = $v['rec_fee'];
                        $list2[$k]['give_fee'] = $v['give_fee'];
                        $list2[$k]['wx_rec_fee'] = $v['wx_rec_fee'];
                        $list2[$k]['wx_give_fee'] = $v['wx_give_fee'];
                        $list2[$k]['wxonline_rec_fee'] = $v['wxonline_rec_fee'];
                        $list2[$k]['wxonline_give_fee'] = $v['wxonline_give_fee'];
                        $list2[$k]['ali_rec_fee'] = $v['ali_rec_fee'];
                        $list2[$k]['ali_give_fee'] = $v['ali_give_fee'];
                        $list2[$k]['alionline_rec_fee'] = $v['alionline_rec_fee'];
                        $list2[$k]['alionline_give_fee'] = $v['alionline_give_fee'];
                        $list2[$k]['cash_rec_fee'] = $v['cash_rec_fee'];
                        $list2[$k]['cash_give_fee'] = $v['cash_give_fee'];
                        $list2[$k]['con_fee'] = $v['con_fee'];
                        $list2[$k]['con_order'] = $v['con_order'];
                        $list2[$k]['give_bonus'] = $v['give_bonus'];
                        $list2[$k]['con_bonus'] = $v['con_bonus'];
                    }
                    $header = ['门店', '领卡数量', '退卡数量', '实充金额', '赠送金额', '微信实充金额', '微信充值赠送金额', '微信自助实充金额', '微信自助充值赠送金额', '支付宝实充金额', '支付宝赠送金额', '支付宝自助实充金额', '支付宝自助充值赠送金额', '现金实充金额', '现金充值赠送金额', '消费金额', '消费次数', '赠送积分', '使用积分']; //表头,名称可自定义
                    $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2, 'cardsurvey_mach' . time() . rand(1000, 9999), "会员概览数据-门店统计", "cardsurvey", false);;

                }
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }
        } else {
            $map['mid'] = ['in', $this->get_mer_mid()];
            $CardFee = new CardUserFee();
            $CardUserGet = new CardUserGet();
            $map['type'] =array(array('eq',1),array('eq',999),"or");
            $map1 = $map;
            $info['rec_fee'] = $CardFee->where($map1)->sum('num');
            $map2 = $map;
            $map2['type'] = 2;
            $info['con_fee'] = $CardFee->where($map2)->sum('num');
            $map3['mid'] = ['in', $this->get_mer_mid()];
            $map3['card_type'] = "MEMBER_CARD";
            $info['sur_fee'] = $CardUserGet->where($map3)->sum('balance');
            $info['sur_bonus'] = $CardUserGet->where($map3)->sum('bonus');
            $machlist = $this->getmchlist();
            $this->assign('info', $info);
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();
        }
    }

    //获取门店列表
//    private function get_machlist(){
//        $Merchant = new Merchant();
//        $mab['id'] = ['in',$this->get_mer_mid()];
//        $mach = $Merchant->where($mab)->select();
//        $this->assign('mach',$mach);
//    }

    //获取card_id
    private function get_card_id()
    {
        $CardMerchant = new CardMember();
        $card_id = $CardMerchant->where(['top_mid' => Session::get('login_top_mid')])->value('card_id');
        return $card_id;
    }

    //会员卡核销记录
    public function reportcardmemberlist(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['mchid'])) {
                $mchid = $param['mchid'];
            } else {
                $mchid = $this->get_mer_mid();
            }
            if (!empty($param['code'])) {
                $map['a.code'] = $param['code'];
            }
            if (!empty($param['action_user'])) {
                $map['a.action_user'] = $param['action_user'];
            }
            if (!empty($param['min_balance'])) {
                $map['a.num'] = ['egt', $param['min_balance']];

            }
            if (!empty($param['max_balance'])) {
                $map['a.num'] = [['egt', $param['min_balance']], ['elt', $param['max_balance']], 'and'];

            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month") - 86400;
                $end_time = time();
            }

            $map['a.mid'] = array('in', $mchid);
            $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            $map['a.type'] = 2;
            if ($param['type'] == "search") {
                $CardUserFee = new CardUserFee();
                $list = $CardUserFee->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->field('a.orderid,a.mid,a.code,a.num,a.source,a.create_time,a.action_user,b.name')
                    ->order('create_time desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                foreach ($list as $k => $v) {
                    $list[$k]['mchname'] = $mchinfo[$v['mid']];
                }
                $re_param["total"] = $CardUserFee->alias('a')->where($map)->count();
                //获取的记录
                $re_param["rows"] = collection($list)->toArray();
                return json($re_param);
            } elseif ($param['type'] == "out_excel") {
                $pub = new \app\common\controller\Pub();
                $header = ['门店', '订单号', '姓名', '卡号', '消费金额', '消费渠道', '消费时间', '操作人']; //表头,名称可自定义
                $CardUserFee = new CardUserFee();
                $list = $CardUserFee->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)
                    ->field('a.orderid,a.mid,a.code,a.num,a.source,a.create_time,a.action_user,b.name')
                    ->order('create_time desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                $list2 = [];
                foreach ($list as $k => $v) {
                    $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                    $list2[$k]['orderid'] = $v['orderid'];
                    $list2[$k]['name'] = $v['name'];
                    $list2[$k]['code'] = $v['code'];
                    $list2[$k]['num'] = $v['num'];
                    $list2[$k]['source'] = $v['source'];
                    $list2[$k]['create_time'] = $v['create_time'];
                    $list2[$k]['action_user'] = $v['action_user'];

                }
                $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2, 'reportcardmemberlist_' . time() . rand(1000, 9999), "会员消费数据", "reportcardmemberlist", false);;
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }


        } else {


            $map['mid'] = array('in', $this->get_mer_mid());
            $map['type'] = 2;
            $CardUserFee = new CardUserFee();
            $info = $CardUserFee->where($map)->field('sum(num) as con_fee,count(id) as con_order')->find();
            $machlist = $this->getmchlist();
            $this->assign('info', $info);
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();
        }
    }


    //会员卡充值记录
    public function cardreglist(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['mchid'])) {
                $mchid = $param['mchid'];
            } else {
                $mchid = $this->get_mer_mid();
            }
            if (!empty($param['code'])) {
                $map['a.code'] = $param['code'];
            }
            if (!empty($param['action_user'])) {
                $map['a.action_user'] = $param['action_user'];
            }
            if (!empty($param['min_balance'])) {
                $map['a.num'] = ['egt', $param['min_balance']];

            }
            if (!empty($param['max_balance'])) {
                $map['a.num'] = [['egt', $param['min_balance']], ['elt', $param['max_balance']], 'and'];

            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month") - 86400;
                $end_time = time();
            }

            $map['a.mid'] = array('in', $mchid);
            $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            $map['a.type'] = 1;

            if ($param['type'] == "search") {
                $CardUserFee = new CardUserFee();
                $list = $CardUserFee->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->field('a.orderid,a.mid,a.code,a.num,a.source,a.remark,a.cate,a.pay_cate,a.create_time,a.action_user,b.name')
                    ->order('a.id desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                foreach ($list as $k => $v) {
                    $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                    $list2[$k]['orderid'] = $v['orderid'];
                    $list2[$k]['name'] = $v['name'];
                    $list2[$k]['code'] = $v['code'];
                    $list2[$k]['num'] = $v['num'];
                    $list2[$k]['pay_cate'] = getpaycate($v['pay_cate']);
                    $list2[$k]['cate'] = getcate($v['cate']);
                    $list2[$k]['remark'] = $v['remark'];
                    $list2[$k]['source'] = $v['source'];
                    $list2[$k]['create_time'] = $v['create_time'];
                    $list2[$k]['action_user'] = $v['action_user'];
                }
                $re_param["total"] = $CardUserFee->alias('a')->where($map)->count();
                //获取的记录
                $re_param["rows"] = $list2;
                return json($re_param);
            } elseif ($param['type'] == "out_excel") {
                $CardUserFee = new CardUserFee();
                $list = $CardUserFee->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)->field('a.orderid,a.remark,a.mid,a.code,a.num,a.source,a.cate,a.pay_cate,a.create_time,a.action_user,b.name')
                    ->order('a.id desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                foreach ($list as $k => $v) {
                    $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                    $list2[$k]['orderid'] = $v['orderid'];
                    $list2[$k]['name'] = $v['name'];
                    $list2[$k]['code'] = $v['code'];
                    $list2[$k]['num'] = $v['num'];
                    $list2[$k]['pay_cate'] = getpaycate($v['pay_cate']);
                    $list2[$k]['cate'] = getcate($v['cate']);
                    $list2[$k]['remark'] = $v['remark'];
                    $list2[$k]['source'] = $v['source'];
                    $list2[$k]['create_time'] = $v['create_time'];
                    $list2[$k]['action_user'] = $v['action_user'];
                }

                $pub = new \app\common\controller\Pub();
                $header = ['门店', '订单号', '姓名', '卡号', '变动金额','充值方式','充值类型','备注','充值渠道', '消费时间', '操作人']; //表头,名称可自定义
                $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2,'cardreglist_' . time() . rand(1000, 9999), "会员充值数据", "cardreglist", false);;
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }

        } else {
            $map['mid'] = array('in', $this->get_mer_mid());

            $condition = "mid in({$this->get_mer_mid()}) and type=1 and cate=1";
            $condition1 = $condition . " and pay_cate=1";
            $condition2 = $condition . " and pay_cate=0";
            $condition3 = $condition . " and pay_cate=3";
            $condition4 = $condition . " and pay_cate=2";
            $condition5 = $condition . " and pay_cate=4";
            $condition6 = "mid in({$this->get_mer_mid()}) and type=1 and cate=3";//充值赠送
            $condition7 = "mid in({$this->get_mer_mid()}) and type=1 and cate in (1,3)";//充值总额
            $CardUserFee = new CardUserFee();
            $con_z_info = $CardUserFee->where($condition7)->field('sum(num) as z_con_fee')->find()->toArray();
            $give_info = $CardUserFee->where($condition6)->field('sum(num) as give_fee,count(id) as give_order')->find()->toArray();
            $con_info = $CardUserFee->where($condition)->field('sum(num) as con_fee,count(id) as con_order')->find()->toArray();
            $wx_info = $CardUserFee->where($condition1)->field('sum(num) as wx_con_fee,count(id) as wx_con_order')->find()->toArray();
            $wxonline_info = $CardUserFee->where($condition2)->field('sum(num) as wxonline_con_fee,count(id) as wxonline_con_order')->find()->toArray();
            $ali_info = $CardUserFee->where($condition3)->field('sum(num) as ali_con_fee,count(id) as ali_con_order')->find()->toArray();
            $alionline_info = $CardUserFee->where($condition4)->field('sum(num) as alionline_con_fee,count(id) as alionline_con_order')->find()->toArray();
            $cash_info = $CardUserFee->where($condition5)->field('sum(num) as cash_con_fee,count(id) as cash_con_order')->find()->toArray();
            //END
            //手续费
            $Paylog = new Paylog();
            $conditions = "mid in({$this->get_mer_mid()}) and pay_cate = 'recharge'";
            $conditionswx = $conditions . " and pay_fs in (0,2)";
            $conditionsali = $conditions . " and pay_fs in (1,3)";
            $wx_infos = $Paylog->where($conditionswx)->field('sum(service_charge) as wx_service_charge')->find()->toArray();
            $ali_infos = $Paylog->where($conditionsali)->field('sum(service_charge) as ali_service_charge')->find()->toArray();
            $wx_info = array_merge($wx_info,$wx_infos);
            $ali_info = array_merge($ali_info,$ali_infos);
            $info = array_merge($con_z_info, $give_info, $con_info, $wx_info, $wxonline_info, $ali_info, $alionline_info, $cash_info);
            $machlist = $this->getmchlist();
            $this->assign('info', $info);
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();

        }
    }

    //积分数据
    public function cardbonuslist(Request $request)
    {
        if ($request->isAjax()){
            $param = $request->post();
            $CardUserBonus = new CardUserBonus();
            if (!empty($param['mchid'])) {
                $mchid = $param['mchid'];
            } else {
                $mchid = $this->get_mer_mid();
            }
            if (!empty($param['code'])) {
                $map['a.code'] = $param['code'];
            }
            if (!empty($param['action_user'])) {
                $map['a.action_user'] = $param['action_user'];
            }

            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month");
                $end_time = time();
            }
            $map['a.create_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            $map['a.mid']= ['in',$mchid];
            if ($param['type'] == 'search') {
                $list = $CardUserBonus->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])
                    ->field('a.orderid,a.mid,a.code,a.num,a.source,a.cate,a.pay_cate,a.create_time,a.action_user,b.name')
                    ->order('a.id desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');
                $list2=[];
                foreach ($list as $k => $v) {
                    $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                    $list2[$k]['orderid'] = $v['orderid'];
                    $list2[$k]['name'] = $v['name'];
                    $list2[$k]['code'] = $v['code'];
                    $list2[$k]['num'] = $v['num'];
                    $list2[$k]['pay_cate'] = getpaycate($v['pay_cate']);
                    $list2[$k]['cate'] = getcate($v['cate']);
                    $list2[$k]['source'] = $v['source'];
                    $list2[$k]['create_time'] = $v['create_time'];
                    $list2[$k]['action_user'] = $v['action_user'];

                }


                $re_param["total"] = $CardUserBonus->alias('a')->where($map)->count();
                //获取的记录
                $re_param["rows"] = $list2;
                return json($re_param);
            }elseif ($param['type'] == 'out_excel') {
                $list = $CardUserBonus->alias('a')
                    ->join('wn_card_user_get b', 'a.code=b.code', 'left')
                    ->where($map)
                    ->field('a.orderid,a.mid,a.code,a.num,a.source,a.cate,a.pay_cate,a.create_time,a.action_user,b.name')
                    ->order('a.id desc')->select();
                $Merchant = new Merchant();
                $mchinfo = $Merchant->where(['id' => ['in', $mchid]])->column('merchant', 'id');

                foreach ($list as $k => $v) {
                    $list2[$k]['mchname'] = $mchinfo[$v['mid']];
                    $list2[$k]['orderid'] = $v['orderid'];
                    $list2[$k]['name'] = $v['name'];
                    $list2[$k]['code'] = $v['code'];
                    $list2[$k]['num'] = $v['num'];
                    $list2[$k]['pay_cate'] = getpaycate($v['pay_cate']);
                    $list2[$k]['cate'] = getcate($v['cate']);
                    $list2[$k]['source'] = $v['source'];
                    $list2[$k]['create_time'] = $v['create_time'];
                    $list2[$k]['action_user'] = $v['action_user'];

                }
                $pub = new \app\common\controller\Pub();
                $header = ['门店', '订单号', '姓名', '卡号', '变动积分','变动方式','变动类型','变动渠道', '变动时间', '操作人']; //表头,名称可自定义
                $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $list2,'cardbonuslist_' . time() . rand(1000, 9999), "会员积分数据", "cardbonuslist", false);;
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }


        }else{
            $CardUserBonus = new CardUserBonus();
            $CardUserGet = new CardUserGet();
            $conditon= 'mid in ('.$this->get_mer_mid().')';
            $conditon1 =$conditon."and type=1";
            $conditonor1 =$conditon."and type=999";
            $conditon2 =$conditon."and type=2";
            $data['put_bonus'] = $CardUserBonus->where($conditon1)->whereOr($conditonor1)->sum('num');
            $data['use_bonus'] = $CardUserBonus->where($conditon2)->sum('num');
            $map['mid'] = ['in',$this->get_mer_mid()];
            $map['card_type'] = 'MEMBER_CARD';
            $data['sur_bonus'] = $CardUserGet->where($map)->sum('bonus');
//            $data['sy_bonus'] = $data['put_bonus']-$data['use_bonus'];
            $machlist = $this->getmchlist();
            $this->assign('info',$data);
//            halt($data);
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();
        }

    }

    //退卡数据
    public function carddellist(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->post();
            if (!empty($param['code'])) {
                $map['code'] = $param['code'];
            }
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month") - 86400;
                $end_time = time();
            }
            $mchid = $this->get_mer_mid();
            $map['del_time'] = array(array('egt', $start_time), array('elt', $end_time), "and");
            $map['mid'] = ['in', $mchid];
            $CardUserGet = new CardUserGet();
            $map['card_type'] = 'MEMBER_CARD';
            $map['have'] = 0;
            if ($param['type'] == 'search') {
                $list = $CardUserGet->where($map)->field('code,card_id,id,bonus,nickname,balance,fire,frequency,del_time')->order('del_time desc')->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])->select();
                foreach ($list as $k=>$v){
                    $list[$k]['fire'] = $v['fire'] == 1?'已激活':'未激活';
                    $list[$k]['del_time'] = date('Y-m-d h:i:s',$v['del_time']);
                }
                $re_param["sql"] = $map;
                $re_param["total"] = $CardUserGet->where($map)->count();
                //获取的记录
                $re_param["rows"] = collection($list)->toArray();
                return json($re_param);

            }elseif ($param['type'] == 'out_excel') {
                $list = $CardUserGet->where($map)->field('code,card_id,phone,bonus,nickname,balance,fire,frequency,del_time')->order('del_time desc')->select();
                $lists = [];
                foreach ($list as $k=>$v){
                    $lists[$k]['code'] = $v['code'];
                    $lists[$k]['nickname'] = $v['nickname'];
                    $lists[$k]['phone'] = $v['phone'];
                    $lists[$k]['fire'] = $v['fire'] == 1?'已激活':'未激活';
                    $lists[$k]['bonus'] = $v['bonus'];
                    $lists[$k]['balance'] = $v['balance'];
                    $lists[$k]['del_time'] = date('Y-m-d h:i:s',$v['del_time']);
                    $lists[$k]['frequency'] = $v['frequency'];
                }
                $pub = new \app\common\controller\Pub();
                $header = ['卡号', '昵称', '手机号', '状态', '积分','余额','退卡时间','使用频次']; //表头,名称可自定义
                $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, $lists,'carddellist_' . time() . rand(1000, 9999), '会员退卡数据', "carddellist", false);;
                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }
        }else{


        return $this->fetch();
        }
    }

    //操作数据
    public function cardoperlog()
    {
        $p_data = request()->get();
        unset($p_data['page']);
        $map = [];
        if ($p_data) {
            if ($p_data['code']) {
                $map['code'] = array('like', '%' . trim($p_data['code']) . '%');
            } else {
                $p_data['code'] = '';
            }
            //门店
            if ($p_data['mid']) {
                $map['mid'] = $p_data['mid'];
            } else {
                $p_data['mid'] = '';
            }
            //时间
            if ($p_data['starttime'] !== '') {
                $starttime = strtotime($p_data['starttime']);
            } else {
                $starttime = 0;
            }
            if ($p_data['endtime'] !== '') {
                $endtime = strtotime($p_data['endtime']) + 86400;
            } else {
                $endtime = strtotime(date('Y-m-d H:i:s')) + 86400;
            }
        } else {
            $p_data['code'] = '';
            $p_data['con_username'] = '';
            $p_data['mid'] = '';
            $starttime = 0;
            $endtime = strtotime(date('Y-m-d H:i:s')) + 86400;
        }
        $map['create_time'] = array('between', '' . $starttime . ',' . $endtime . '');
        $CardMember = new CardMember();
        $CardUserUpdate = new CardUserUpdate();
        $card_id = $CardMember->where(['top_mid' => Session::get('login_top_mid')])->value('card_id');
        $map['card_id'] = $card_id;
        $list = $CardUserUpdate->where($map)->order('id desc')->paginate('20', false, ['query' => request()->param()]);
        $this->assign('list', $list);
        $this->assign('p_data', $p_data);
        $this->get_machlist();
        return $this->fetch();
    }

    //门店列表
    private function get_machlist()
    {
        //门店
        $Merchant = new Merchant();
        $mab['id'] = ['in', $this->get_mer_mid()];
        $mach = $Merchant->where($mab)->select();
        $this->assign('mach', $mach);
    }

}
